package com.xzy.generic.plus;

import com.xzy.generic.plus.data.Aaa;
import com.xzy.generic.plus.data.Bbb;
import com.xzy.generic.plus.data.Ccc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.EventListener;

/**
 * @author xzy.xiao
 * @date 2023/3/13  15:41
 */
@Slf4j
public class Main {

    @EventListener
    public void onAaaEvent(GenericEvent<Aaa> event) {
        log.info("onAaaEvent 监听到事件 => {}", event);
    }

    @EventListener
    public void onBbbEvent(GenericEvent<Bbb> event) {
        log.info("onBbbEvent 监听到事件 => {}", event);
    }

    @EventListener
    public void onCccEvent(GenericEvent<Ccc> event) {
        log.info("onCccEvent 监听到事件 => {}", event);
    }

    public static void main(String[] args) {
        // 创建容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 注册事件监听器
        applicationContext.registerBean("genericEventPublisher", GenericEventPublisher.class);
        applicationContext.registerBean("main", Main.class);
        applicationContext.refresh();

        // 发布事件
        for (int i = 0; i < 3; i++) {
            GenericEventPublisher.sendGenericEvent(Main.class, new Aaa());
            GenericEventPublisher.sendGenericEvent(Main.class, new Bbb());
            GenericEventPublisher.sendGenericEvent(Main.class, new Ccc());
        }
    }
}
